/*
    This file is part of ciberRatoToolsSrc.

    Copyright (C) 2001-2006 Universidade de Aveiro

    ciberRatoToolsSrc is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    ciberRatoToolsSrc is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "crqcontrolpanel.h"
#include "../crqcanvas.h"

#include <qframe.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qspinbox.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>

/* 
 *  Constructs a controlPanel which is a child of 'parent', with the 
 *  name 'name' and widget flags set to 'f' 
 */
CRQControlPanel::CRQControlPanel(CRQCanvas *commCanvas, CRQComm *comm, 
  QString skinFileName, char soundStatus, QWidget* parent,
  const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{
	controlComm = comm;
	sound = soundStatus;

	skin(skinFileName);

	//setBackgroundPixmap(QPixmap(backFile));	
	setBackgroundColor(QColor(200, 200, 200));	

    //QPixmap icon( ( const char** ) controlpanel_xpm );
    //setIcon( icon );
	//setMaximumSize( 160, 32 );
	setGeometry( 8, 30, 160, 32 );
    //setCaption( tr( "Control Panel"  ) );
    //if ( !name )
	//setName( "controlPanel" );

    stopButton = new QPushButton( this, "stopButton" );
    stopButton->setGeometry(0, 0, 32, 32); 
	stopButton->setPixmap( QPixmap(stopFile) );
	connect( stopButton, SIGNAL( clicked() ), this, SLOT( stop() ) );
	
    startButton = new QPushButton( this, "startButton" );
    startButton->setGeometry( 40, 0, 32, 32 ); 
	startButton->setPixmap( QPixmap(startFile) ); 
	connect( startButton, SIGNAL( clicked() ), this, SLOT( start() ) );

/*
    linhaVcp = new QFrame( this, "linhaVcp" );
    linhaVcp->setGeometry( QRect( 145, 55, 16, 230 ) ); 
    linhaVcp->setFrameStyle( QFrame::VLine | QFrame::Sunken );
	linhaVcp->setBackgroundOrigin( ParentOrigin );
	linhaVcp->setBackgroundPixmap( QPixmap(backFile) );
*/
    spinRobots = new QSpinBox( this, "spinRobots" );
    spinRobots->setGeometry( 80, 6, 40, 20 ); 
	spinRobots->setMaxValue( commCanvas->nRobots );
	spinRobots->setMinValue( 1 );
	spinRobots->setValue( 1 );
	//spinRobots->setPrefix( "Robot Id:     " );

    removeButton = new QPushButton( this, "removeButton" );
    removeButton->setGeometry( 128, 0, 32, 32 ); 
    removeButton->setText( tr( ""  ) );
    removeButton->setPixmap( QPixmap( removeFile) );
	connect( removeButton, SIGNAL( clicked() ), this, SLOT( remove() ) );

//	showMaximized();
}

/*  
 *  Destroys the object and frees any allocated resources
 */
CRQControlPanel::~CRQControlPanel()
{
    // no need to delete child widgets, Qt does it all for us
}

void CRQControlPanel::skin(QString skinFileName)
	{
	FILE *fp = NULL;
	char skinName[32];

	//cout << "CRQControlPanel::skin skin name = " << skinFileName << endl;

    fp = fopen(skinFileName, "r");
	if(fp == NULL)
		{
		cerr << "Unable to load file " << skinFileName << endl;
		}
	else
		{
		fscanf(fp, "%s", skinName);
		strcpy(backFile, "skins/");
		strcat(backFile, skinName);
		strcat(backFile, "/controlbg.png");

		strcpy(startFile, "skins/");
		strcat(startFile, skinName);
		strcat(startFile, "/start.png");

		strcpy(stopFile, "skins/");
		strcat(stopFile, skinName);
		strcat(stopFile, "/stop.png");

		strcpy(removeFile, "skins/");
		strcat(removeFile, skinName);
		strcat(removeFile, "/remove.png");
		}

	fclose(fp);

	}
/*  
 *  Main event handler. Reimplemented to handle application
 *  font changes
 */
bool CRQControlPanel::event( QEvent* ev )
{
    bool ret = QWidget::event( ev ); 
    if ( ev->type() == QEvent::ApplicationFontChange ) {
	QFont stopButton_font(  stopButton->font() );
	stopButton_font.setFamily( "lucida" );
	stopButton_font.setPointSize( 24 );
	stopButton_font.setBold( TRUE );
	stopButton->setFont( stopButton_font ); 
	QFont startButton_font(  startButton->font() );
	startButton_font.setFamily( "lucida" );
	startButton_font.setPointSize( 24 );
	startButton_font.setBold( TRUE );
	startButton->setFont( startButton_font ); 
	QFont controlPanelText_font(  controlPanelText->font() );
	controlPanelText_font.setFamily( "lucida" );
	controlPanelText_font.setPointSize( 18 );
	controlPanelText_font.setItalic( TRUE );
	controlPanelText->setFont( controlPanelText_font ); 
    }
    return ret;
}

void CRQControlPanel::stop( void )
{
	controlComm->sendMessage( "<Stop/>" );
}


void CRQControlPanel::start( void )
{
	controlComm->sendMessage( "<Start/>" );
	//TODO 2005 if(QSound::isAvailable() && sound == 'y')
	if(QSound::available() && sound == 'y')
		QSound::play("sounds/begin.wav");
}


void CRQControlPanel::remove( void )
{
	QString rmsg("<Robot Removed=\"Yes\" Id=\"" );
	QString val;
	val.setNum( spinRobots->value() );
	rmsg.append( val );
	rmsg.append( "\"/>" );	

	controlComm->sendMessage( (char *) rmsg.latin1() );

}

void CRQControlPanel::closeEvent( QCloseEvent *ev)
{
	ev->ignore();
}
